﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyUAMS.BLL;
using MyUAMS.DAL;

public partial class MyWork : System.Web.UI.Page
{
    WorkServer assetSrv = new WorkServer();
    protected void Page_Load(object sender, EventArgs e)
    {
        //username.Text = "您好，" + Session["CustomerName"].ToString();
        if (!IsPostBack)
        {
            Bind();
            
            if (Session["CustomerId"] != null)
            {
                int uid = int.Parse(Session["CustomerId"].ToString());
                if (assetSrv.GetInformationByUid(uid) == true)
                {
                    informationbtn.ImageUrl = "../Image/有消息.png";
                }
            }
            else
            {
                lblProErr.Text = "会话丢失";
            }
            
        }
        
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int assetId = 0;
        GridView gvProduct = new GridView();
        gvProduct = (GridView)Page.FindControl("dgvAsset");
        if(gvProduct != null)
        {
            for(int i = 0; i < gvProduct.Rows.Count; i++)
            {
                CheckBox chkChoice = new CheckBox();
                chkChoice = (CheckBox)gvProduct.Rows[i].FindControl("chkChoice");
                if(chkChoice != null)
                {
                    if (chkChoice.Checked)
                    {
                        assetId = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                        assetSrv.DeletePro(assetId);
                    }
                }
            }
            Bind();
        }
    }
    protected void Bind()
    {
        var products = assetSrv.GetAllProduct();
        if (products.Count() == 0)
        {
            lblProErr.Text = "校园还未拥有资产,请添加！";
        }
        else
        {
            lblProErr.Text = " ";
        }
        dgvAsset.DataSourceID = "ldsAsset";
        dgvAsset.DataBind();
    }
    protected string GetCategoryName(object categoryId)
    {
        if (categoryId == null) return string.Empty; // 防止空值

        int id = Convert.ToInt32(categoryId); // 将对象转换为整数

        using (var context = new DataClasses1DataContext())
        {
            // 查询类别名称
            var category = context.AssetType_Table.SingleOrDefault(c => c.Tid == id);
            return category != null ? category.Tname : "未知类别"; // 返回类别名称或默认值
        }
    }


    protected void ToExcel_Click(object sender, EventArgs e)
    {
        // 创建一个新的Excel工作簿
        HttpResponse response = HttpContext.Current.Response;

        response.Clear();
        response.Charset = "";
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=总资产名单.xls");
        dgvAsset.Columns[0].Visible = false;
        dgvAsset.AllowPaging = false;
        dgvAsset.DataBind ();

        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            // 把GridView的内容输出到HtmlTextWriter
            dgvAsset.RenderControl(htw);

            // 写入到响应流
            response.Write(sw.ToString());
            response.End();
            dgvAsset.AllowPaging = true;
            dgvAsset.DataBind();
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // 确保GridView在<form runat="server">中渲染
    }

    protected void informationbtn_Click(object sender, ImageClickEventArgs e)
    {
        informationbtn.ImageUrl = "../Image/消息.png";
        Response.Redirect("../MyInformation/MyAllinformation.aspx");
    }
}
